package com.open4cn.simulator.parser.xrite.x7800;

import com.open4cn.simulator.answer.xrite.x7800.CurmodeSetAction;
import com.open4cn.simulator.answer.xrite.x7800.CurmodeSetAnswer;
import com.open4cn.simulator.core.answer.AbstractAnswer;
import com.open4cn.simulator.core.answer.AnswerResponse;
import com.open4cn.simulator.core.ext.InMessage;
import com.open4cn.simulator.core.ext.OutMessage;
import com.open4cn.simulator.core.ext.XriteStatus;
import com.open4cn.simulator.core.parser.IInstructionParser;
import com.open4cn.simulator.core.parser.InstructionParserChain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.List;

/**
 * @author yinxiaopeng
 * @version 1.0.0
 * @company 致景科技
 * @department 飞梭BU/研发
 * @description
 * @Email yinxiaopeng@zj.tech
 * @date 2022-07-21 16:34
 */
public class CurmodeSet implements IInstructionParser {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private static final String COMMAND = "meas set curmode @";

    private CurmodeSetAnswer answer = new CurmodeSetAnswer();

    private XriteStatus xriteStatus;

    public CurmodeSet(XriteStatus xriteStatus) {
        this.xriteStatus = xriteStatus;
        init();
    }

    private void init() {
        answer.setActions(Arrays.asList(new CurmodeSetAction()));

    }

    @Override
    public Object parse(Object msg, InstructionParserChain chain) {
        InMessage inMessage = (InMessage) msg;
        if (!match(inMessage.getCommand())) {
            return chain.filter(msg);
        }

        XriteStatus.SpecularGlossEnum specularGlossEnum = changeApertureTo(inMessage.getCommand());

        AnswerResponse response = this.answer.answer(xriteStatus, specularGlossEnum, inMessage.getCommand());
        return new OutMessage(response.getData().toString());
    }

    @Override
    public List<AbstractAnswer> getAnswers() {
        return Arrays.asList(answer);
    }


    /**
     * 匹配
     *
     * @param instruction
     * @return
     */
    private boolean match(String instruction) {
        if (null == instruction) {
            return false;
        }

        if (COMMAND.length() > instruction.length()) {
            return false;
        }

        for (int i = 0, len = COMMAND.length(); i < len; i++) {
            String charAt = String.valueOf(COMMAND.charAt(i));
            if ("$".equals(charAt)) {
                continue;
            }
            if ("@".equals(charAt)) {
                break;
            }
            if (COMMAND.charAt(i) != instruction.charAt(i)) {
                return false;
            }
        }
        return true;
    }


    private XriteStatus.SpecularGlossEnum changeApertureTo(String instruction) {
        if (instruction.endsWith(XriteStatus.SpecularGlossEnum.SCI.getTag())) {
            return XriteStatus.SpecularGlossEnum.SCI;
        }
        if (instruction.endsWith(XriteStatus.SpecularGlossEnum.SCE.getTag())) {
            return XriteStatus.SpecularGlossEnum.SCE;
        }
        return null;
    }
}
